// shopPages/archives/archives.js
var app = getApp();
import {
  tags_list,
  update_goodsInfo,
  ftp_file,
  baseUrl,
  del_file,
  del_user_img,
  get_goodsInfo,
  del_goods_img,
  ftp_goodsFile
} from "../../utils/api.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Pictuer: {
      type: 0,
      srcList: []
    },
    imgs: []
  },
  //添加图片
  selectFile1: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.p_index;
    var list = that.data.list;
    wx.chooseImage({
      count: 6,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var imgList = list[index].imgs;
        for (var i in tempFilePaths) {
          imgList.push(tempFilePaths[i])
        }
        list[index].imgs = imgList;
        that.setData({
          list: list
        })
      }
    })
  },
  //删除图片
  delImg: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.index);
    var ix = e.currentTarget.dataset.index;
    var list = that.data.imgs;

    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (res) {
        if (res.confirm) {
          list.splice(ix, 1);
        } else if (res.cancel) {
          return false
          console.log('用户点击取消')
        }
        that.setData({
          imgs: list
        });
      }
    })
  },

  // 照片选择图片
  selectFile: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.p_index;

    var imglen = that.data.Pictuer.srcList.length;
    wx.chooseImage({
      count: 4, // 默认9  
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        console.log(res);
        console.log(res.tempFilePaths.length);
        if (res.tempFilePaths.length > 0) {
          //图如果满了9张，不显示加图
          for (let i = 0; i < res.tempFilePaths.length; i++) {
            that.ftpFile({
              type: index,
              src: res.tempFilePaths[i],
              // uid: that.data.userInfo.id,
            })

          }
        }
      }
    })

  },
  ftpFile: function (params) {
    const that = this
    var list = that.data.list;
    wx.showLoading({
      title: '上传中',
    })
    wx.uploadFile({
      url: baseUrl + ftp_goodsFile, // 仅为示例，非真实的接口地址
      filePath: params.src,
      name: 'file',
      formData: {},
      success(res) {
        wx.hideLoading();
        let json_res = JSON.parse(res.data)
        if (json_res.code != 1) {
          app.showTipMsg('上传出错');
        } else {
          var index = params.type;
          var imgList = that.data.imgs;
          imgList.push(json_res.url)
        
          that.setData({
            imgs: imgList
          })

        }
      },
      fail(err) {
        console.log(err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    wx.showLoading({
      title: '玩命加载中',
    })

    var url2 = '/Manager/info';
    let params = {
      uid: app.globalData.userInfo.id,
      fields:'qualifications_img'
    };
    app.HttpPostSend(url2, params, function (res) {
      wx.hideLoading();
      if (res.code ==0) {
        app.showTipMsg(res.msg);

      } else {
        that.setData({
          imgs:res.data
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    const that = this;

    var url2 = '/Manager/update_info';
    let params = {
      uid: app.globalData.userInfo.id,
      fields: 'qualifications_img',
      dataarr:that.data.imgs
    };
    app.HttpPostSend(url2, params, function (res) {
      
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})